package com.ldk.framework.service.impl;

import java.util.Date;
import java.util.List;

import com.ldk.base.framework.util.ConvertUtil;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.*;
import com.ldk.framework.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ldk.base.framework.dao.IBaseDAO;
import com.ldk.base.framework.service.impl.BaseService;
import com.ldk.framework.dao.IDriverDAO;

/**
* DriverService接口实现类
* @author xiangwl
*/
@Service
public class DriverService extends BaseService<Driver> implements IDriverService {

	@Autowired
	private IDriverDAO driverDAO;

	@Autowired
	protected IDictService dictService;
	@Autowired
	protected IDictdataService dictdataService;
	@Autowired
	protected ICouponService couponService;
	@Autowired
	protected IUserService userService;

	@Override
	protected IBaseDAO<Driver> getBaseDao() {
     	return this.driverDAO;
	}
	
	@Override
	public Boolean existByOpenId(String dbKey, String openId) {
		try{
			Driver query = new Driver();
			query.setOpenId(openId);
			query.setEnabled(true);
			
			long count = selectCount(dbKey, query);
			return count > 0;
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return false;
	}

	@Override
	public Driver getDriverByOpenId(String dbKey, String openId) {
		try{
			Driver query = new Driver();
			query.setOpenId(openId);
			query.setEnabled(true);
			
			return selectOne(dbKey, query);
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}
	
	@Override
	public LinkDriver getLinkDriver(String dbKey, String openId) {
		try{
			return this.driverDAO.getLinkDriver(dbKey, openId);
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}
	@Override
	public List<Driver> searchByDistance(String dbKey, Driver query, double distance, Pageable pageable){
		try{
			return this.driverDAO.searchByDistance(dbKey, query, distance, pageable);
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

	/**
	 * 生成6位固定ID号
	 */
	@Override
	public long generateDCommonId() {
		long dCommonId = this.generateID(6);
		while (true) {
			Driver query = new Driver();

			//query.setdCommonId(dCommonId);
			long count = selectCount("", query);

			if(count > 0){
				dCommonId = this.generateID(6);
			}else{
				break;
			}
		}
		return dCommonId;
	}

	@Override
	public Boolean existDCommonId(long dCommonId) {
		Driver query = new Driver();

		//query.setdCommonId(dCommonId);
		long count = selectCount("", query);

		if(count > 0){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public void insertCoupon(Driver driver) {
		/*long rCommonId = driver.getrCommonId();
		String commonId = ConvertUtil.parseString(rCommonId);
		Date cur_date = new Date();
		long createTime = SystemUtil.getAllSeconds(cur_date);
		if(commonId.length() ==6) {
			//A=>B
			//A
			Dictdata query = new Dictdata();
			query.setDataId(15);  //推荐司机返现金卷(元)
			Dictdata data1 = dictdataService.selectByKey("",query);

			//B
			Dictdata query2 = new Dictdata();
			query2.setDataId(16);  //被推荐司机返现金卷(元)
			Dictdata data2 = dictdataService.selectByKey("",query2);

			//1、奖励推荐人
			// 推荐人信息
			Driver query_driver = new Driver();
			query_driver.setdCommonId(driver.getrCommonId());
			Driver driver2 = selectOne("", query_driver);

			//奖励现金券
			Coupon entity = new Coupon();
			entity.setUserId(driver2.getDriverId());
			entity.setCouponType(1);  //现金券
			entity.setAmount(ConvertUtil.parseDouble(data1.getDataValue()));
			entity.setCreateTime(createTime);
			entity.setStatus(0);
			int row = couponService.insert("", entity);

			//修改现金券余额
			Driver entity_driver= new Driver();
			entity_driver.setDriverId(driver2.getDriverId());
			entity_driver.setCashCoupon(driver2.getCashCoupon() + ConvertUtil.parseDouble(data1.getDataValue()));  //更新现金卷总额
			int row2 = updateByKey("", entity_driver);

			//2、奖励被推荐人
			//奖励现金券
			Coupon entity2 = new Coupon();
			entity2.setUserId(driver.getDriverId());
			entity2.setCouponType(1);  //现金券
			entity2.setAmount(ConvertUtil.parseDouble(data2.getDataValue()));
			entity2.setCreateTime(createTime);
			entity2.setStatus(0);
			int row3 = couponService.insert("", entity2);

			//修改现金券余额
			Driver entity_driver2 = new Driver();
			entity_driver2.setDriverId(driver.getDriverId());
			entity_driver2.setCashCoupon(driver.getCashCoupon() + ConvertUtil.parseDouble(data2.getDataValue()));  //更新现金卷总额
			int row4 = updateByKey("", entity_driver2);

		} else  if(commonId.length() ==8) {
			Dictdata query = new Dictdata();
			query.setDataId(17);  //乘客返优惠卷(元)
			Dictdata data1 = dictdataService.selectByKey("",query);

			Dictdata query2 = new Dictdata();
			query2.setDataId(18);  //被推荐司机返现金卷(元)
			Dictdata data2 = dictdataService.selectByKey("",query2);

			Dictdata query3 = new Dictdata();
			query3.setDataId(11);  //优惠券过期时间(单位:天)
			Dictdata data3 = dictdataService.selectByKey("",query3);

			//1、奖励推荐人
			// 推荐人信息
			User query_user = new User();
			query_user.setuCommonId(driver.getrCommonId());
			User user = userService.selectOne("", query_user);

			//奖励优惠券
			Coupon entity = new Coupon();
			entity.setUserId(user.getUserId());
			entity.setCouponType(0);  //优惠劵
			entity.setAmount(ConvertUtil.parseDouble(data1.getDataValue()));
			entity.setCreateTime(createTime);

			//设置过期时间
			long expireTime = SystemUtil.getAddDaysAllSeconds(cur_date,ConvertUtil.parseInt(data3.getDataValue()));
			entity.setExpireTime(expireTime);

			entity.setStatus(0);
			int row = couponService.insert("", entity);

			//修改优惠卷总额
			User entity_user= new User();
			entity_user.setUserId(user.getUserId());
			entity_user.setCoupon(user.getCoupon() + ConvertUtil.parseDouble(data1.getDataValue()));
			int row2 = userService.updateByKey("", entity_user);

			//2、奖励被推荐人
			//奖励现金券
			Coupon entity2 = new Coupon();
			entity2.setUserId(driver.getDriverId());
			entity2.setCouponType(1);  //现金券
			entity2.setAmount(ConvertUtil.parseDouble(data2.getDataValue()));
			entity2.setCreateTime(createTime);
			entity2.setStatus(0);
			int row3 = couponService.insert("", entity2);

			//修改现金券余额
			Driver entity_driver2 = new Driver();
			entity_driver2.setDriverId(driver.getDriverId());
			entity_driver2.setCashCoupon(driver.getCashCoupon() + ConvertUtil.parseDouble(data2.getDataValue()));  //更新现金卷总额
			int row4 = updateByKey("", entity_driver2);
		}*/
	}

	@Override
	public List<DriverBindCar> findBindDriverList(String dbKey) {
		return driverDAO.findBindDriverList(dbKey);
	}

	@Override
	public List<DriverBindCar> findVehicleList(String dbKey) {

		return driverDAO.findVehicleList(dbKey);
	}

	/**
	 * 查询后台统计分析
	 * @param dbKey
	 * @param companyId 为空查询所有
	 * @return
	 */
	@Override
	public CountBean countAnalysis(String dbKey,String companyId) {
		return driverDAO.countAnalysis(dbKey,companyId);
	}
}
